package com.astronomy.article.service;

import com.astronomy.article.mapper.ColumnMapper;
import com.astronomy.article.pojo.Column;
import com.astronomy.entity.IdWorker;
import com.astronomy.entity.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class ColumnService {

    @Autowired
    private ColumnMapper columnMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 全部数据
     * @return
     */
    public List<Column> findAll(){
        return columnMapper.selectAll();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Column findById(String id){
        return  columnMapper.selectByPrimaryKey(id);
    }

    /**
     * 增加
     * @param column
     */
    public void add(Column column){
        column.setId(idWorker.nextId()+"");
        columnMapper.insert(column);
    }

    /**
     * 修改
     * @param column
     */
    public void update(Column column){
        columnMapper.updateByPrimaryKey(column);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id){
        columnMapper.deleteByPrimaryKey(id);
    }


    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    public Page<Column> findPage(int page, int size){
        PageHelper.startPage(page,size);
        return (Page<Column>)columnMapper.selectAll();
    }

    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    public List<Column> findList(Map<String, Object> searchMap){
        Example example = createExample(searchMap);
        return columnMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Column.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // ID
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andLike("id","%"+searchMap.get("id")+"%");
           	}
            // 专栏名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
           	}
            // 专栏简介
            if(searchMap.get("summary")!=null && !"".equals(searchMap.get("summary"))){
                criteria.andLike("summary","%"+searchMap.get("summary")+"%");
           	}
            // 用户ID
            if(searchMap.get("userid")!=null && !"".equals(searchMap.get("userid"))){
                criteria.andLike("userid","%"+searchMap.get("userid")+"%");
           	}
            // 状态
            if(searchMap.get("state")!=null && !"".equals(searchMap.get("state"))){
                criteria.andLike("state","%"+searchMap.get("state")+"%");
           	}


        }
        return example;
    }

    /**
     * 条件+分页查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    public PageResult<Column> findPage(Map<String, Object> searchMap, int page, int size){
        Example example = createExample(searchMap);
        PageHelper.startPage(page,size);
        List<Column> columns = columnMapper.selectByExample(example);
        PageInfo<Column> columnPageInfo = new PageInfo<>(columns);
        return new PageResult<>(columnPageInfo.getTotal(),columnPageInfo.getList());
    }

}
